<?php
namespace Mobile\Controller;

class EvaluationController extends MobileBaseController {

    //商品分类展示
    public function modellist()
    {
        $f_id = I('f_id',844); //一级分类ID

        //左侧栏目
        $menu_list = M('goods_category')->field("id,name")->where("parent_id = $f_id")->order("sort_order asc")->select();
        $this->assign('menu_list',$menu_list);

        //分类商品选项
        $s_id = I('s_id'); //二级分类ID
        if(!$s_id){
            $s_id = $menu_list[0]['id'];
        }

        $goods_list = M('goods_category')->field("id,name")->where("parent_id = $s_id")->order("sort_order asc")->select();
        $this->assign('goods_list',$goods_list);

        $this->assign('f_id',$f_id);
        $this->assign('s_id',$s_id);

        $this->display();
    }

    //估价选项展示
    public function compute()
    {
        $f_id = I('f_id',0); //一级分类ID
        $s_id = I('s_id',0); //二级分类ID
        $t_id = I('t_id',0); //三级分类ID
        $goods_cat = M('goods_category')->field("id,name")->where("id = $t_id")->find();
        //值不存在
        if(!$goods_cat){
            echo "参数错误";
            exit;
        }

        //估价选项ID
       switch ($f_id)
        {
            case 844:
                $evaluation_id = 1;
                break;
            case 845:
                $evaluation_id = 1;
                break;
            case 846:
                $evaluation_id = 1;
                break;
            case 847:
                $evaluation_id = 2;
                break;
            default:
                $evaluation_id = 0;
        }

        //估价选项内容
          $evaluation_cat = M('evaluation_cat')->field("cat_id,cat_name")->where("category_id = $t_id and parent_id=0")->find();
          $evaluation_cat = M('evaluation_cat')->field("cat_id,cat_name")->where("parent_id =".$evaluation_cat['cat_id'])->select();
        //$evaluation_cat = M('evaluation_cat')->field("cat_id,cat_name")->where("parent_id = $evaluation_id")->select();
        if(!$evaluation_cat){
            echo "估价选项为空";
            exit;
        }
        foreach($evaluation_cat as $key => $val){
            $evaluation_cat[$key]["child"] = M('evaluation_cat')->field("cat_id,cat_name")->where("parent_id = {$evaluation_cat[$key]["cat_id"]}")->select();
        }

        $this->assign('goods_cat',$goods_cat);  //商品信息
        $this->assign('evaluation_cat',$evaluation_cat);  //估价选项内容
        $this->assign('f_id',$f_id);  //一级ID
        $this->assign('s_id',$s_id);  //二级ID
        $this->assign('t_id',$t_id);  //三级分类ID
        $this->assign('evaluation_id',$evaluation_id);//估价选项ID
        $this->display();

    }


    //估价计算页面
    public function computefinish()
    {
 
      
        $f_id = I('f_id',0); //一级分类ID
        $s_id = I('s_id',0); //二级分类ID
        $t_id = I('t_id',0); //三级分类ID
        $evaluation_id_arr = I('evaluation_id'); //选项ID

        if(!$f_id && !$s_id && !$t_id && !$evaluation_id_arr){
            echo "参数为空，重新提交";
            exit;
        }

        //获取商品信息
        $goods_cat = M('goods_category')->field("id,name,evaluation_money")->where("id = $t_id")->find();
        $goods_evaluation_money = $goods_cat['evaluation_money'];

        //扣除价格统计
        $deduct_money = 0; //扣除金额
        $evaluation_id_text = implode(",",$evaluation_id_arr);
        $deduct_money_array = M('evaluation_cat')->getField("cat_id,deduct_money");
        foreach($evaluation_id_arr as $key => $val){
            $deduct_money = $deduct_money + $deduct_money_array[ $val ];
        }
        //扣除后的价格
        $new_money = $goods_evaluation_money - $deduct_money;
        $this->assign('f_id',$f_id);  //一级ID
        $this->assign('s_id',$s_id);  //二级ID
        $this->assign('t_id',$t_id);  //三级分类ID
        $this->assign('goods_cat',$goods_cat);  //商品信息
        $this->assign('evaluation_id_text',$evaluation_id_text);  //三级分类ID
        $this->assign('new_money',$new_money);

        $this->display();
    }

    //估价计算页面信息提交
    public function computefinishHandle()
    {
        $data = I("post.");

        if(!$data['f_id'] && !$data['s_id'] && !$data['t_id'] && !$data['evaluation_id_text']){
            echo "参数为空，重新提交";
            exit;
        }

        if($data['do_type'] == 1) {
            $data['mobile'] = $data['mobile1'];
            if (!$data['mobile']) {
                echo "手机号不能为空";
                exit;
            }
        }

        if($data['do_type'] == 2){
            $data['mobile'] = $data['mobile2'];
            if (!$data['mobile']) {
                echo "手机号不能为空";
                exit;
            }
            if(!$data['shop_address']) {
                echo "上门地址不能为空";
                exit;
            }
        }

        $data['add_time'] = time();

        //获取商品分类信息
        $goods_category_array = M('goods_category')->getField("id,name");
        $data['f_name'] = $goods_category_array[ $data['f_id'] ];
        $data['s_name'] = $goods_category_array[ $data['s_id'] ];
        $data['t_name'] = $goods_category_array[ $data['t_id'] ];

        //获取商品信息
        $goods_cat = M('goods_category')->field("id,name,evaluation_money")->where("id = {$data['t_id']}")->find();
        $goods_evaluation_money = $goods_cat['evaluation_money'];
        $data['goods_evaluation_money'] = $goods_evaluation_money;

        //扣除价格统计
        $deduct_money = 0; //扣除金额
        $evaluation_money_arr = array();
        $evaluation_id_arr = explode(",",$data['evaluation_id_text']);
        $deduct_money_array = M('evaluation_cat')->getField("cat_id,deduct_money");
        foreach($evaluation_id_arr as $key => $val){
            $deduct_money = $deduct_money + $deduct_money_array[ $val ];
            array_push($evaluation_money_arr,$deduct_money_array[ $val ]);
        }
        $data['deduct_money'] = $deduct_money;
        $evaluation_money_text = implode('|||',$evaluation_money_arr);
        $data['evaluation_money_text'] = $evaluation_money_text;

        //扣除后的价格
        $new_money = $goods_evaluation_money - $deduct_money;
        $data['new_money'] = $new_money;

        //评估文字内容
        $evaluation_name_arr = array();
        $evaluation_cat_arr = M('evaluation_cat')->getField("cat_id,cat_name");
        foreach($evaluation_id_arr as $key => $val){
            array_push($evaluation_name_arr,$evaluation_cat_arr[ $val ]);
        }
        $evaluation_name_text = implode('|||',$evaluation_name_arr);
        $data['evaluation_name_text'] = $evaluation_name_text;

        //添加记录
        $add_id = M('evaluation')->add($data);

        if($add_id){
            $this->sendEmail();
            $this->success("操作成功",U('/Mobile/Evaluation/modellist'));
        }else{
            $this->error("操作失败",U('/Mobile/Evaluation/compute',array('f_id'=>$data['f_id'],'s_id'=>$data['s_id'],'t_id'=>$data['t_id'])));
        }

    }

    /*
     * 发送邮件
     */
    public function sendEmail()
    {
        $param['to_eamil'] = '2269710968@qq.com';
        $send_email_stats = send_email($param['to_eamil'], '后台测试', '测试发送验证码');
//        if($send_email_stats){
//            echo "发送邮件成功！";
//        }else{
//            echo "发送邮件失败！";
//        }
    }

}